<?php
/**
 * Created by PhpStorm.
 * User: zhangchangshun
 * Date: 17-3-20
 * Time: 下午3:45
 */
class longarticle extends API_Controller
{
    public function __construct()
    {
        parent::__construct();
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        ini_set('date.timezone','Asia/Shanghai'); // 'Asia/Shanghai'

        $this->load->model('long_article_model', 'mlarticle');
        $this->load->model('circle_model','mcircle');
        $this->load->model('text_zan_model', 'mtzan');
        $this->load->model('pay_logs_model', 'mpay_log');
        $this->load->model('account_model','maccount');
        $this->load->model('balance_detail_model','mbalance');
        $this->load->model('upload_model','mupload');
        $this->load->model('comments_model','mcomments');
        $this->load->model('collects_model', 'mcollects');
        $this->load->model('praise_model', 'mpraise');
        $this->load->model('maxmoney_model', 'mmaxmoney');
    }

    /**
     * 长文章详情（发布人）
     */
    public function longarticle_detail(){
        $article_id = $this->input->post('id');
        $article_info = $this->mlarticle->get_one(array('id'=>$article_id,'is_deleted'=>0));
        if(empty($article_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '文章不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            if($article_info['uid']==$this->userinfo['id']){
                $info['is_can']=1;
            }else{
                $info['is_can']=0;
            }
            //是否收藏
            $my_collect = $this->mcollects->get_one(array('uid'=>$this->userinfo['id'],'tid'=>$article_id,'is_deleted'=>1));
            if(!empty($my_collect)){
                $info['is_collect']=0;
            }else{
                $info['is_collect']=1;
            }
            //是否点赞
            $my_concerns = $this->mpraise->get_one(array('uid'=>$this->userinfo['id'],'tid'=>$article_id,'is_deleted'=>1));
            if(!empty($my_concerns)){
                $info['is_praise']=0;
            }else{
                $info['is_praise']=1;
            }
            $text_users = array();
            $text_zans = $this->db->query("select * from boc_text_zan where tid=$article_id order by created desc limit 5")->result_array();
            if(!empty($text_zans)){
                foreach($text_zans as $val){
                    $zan_user_info = $this->maccount->get_one(array('id'=>$val['uid']));
                    $u_photo = $this->mupload->get_one(array('id'=>$zan_user_info['photo']));
                    if(!empty($u_photo)){
                        $text_users[] = UPLOAD_URL.$u_photo['url'];
                    }else{
                        $text_users[] = '';
                    }
                }
            }
            $info['zan_user'] = $text_users;
            $info['content'] = $article_info['content'];
            $info['title'] = $article_info['title'];
            $user_info = $this->maccount->get_one(array('id'=>$article_info['uid']));
            $info['uname'] = $user_info['uname'];
            $photo = $this->mupload->get_one(array('id'=>$user_info['photo']));
            if(!empty($photo)){
                $info['headimg'] = UPLOAD_URL.$photo['url'];
            }else{
                $info['headimg'] = '';
            }
            $info['id'] = $article_id;
            $info['cre_time'] = date('m-d H:i',$article_info['timeline']);
            $info['repeats'] = $article_info['repeats'];
            $info['uid'] = $article_info['uid'];
            $info['comments'] = $article_info['comments'];
            $info['collects'] = $article_info['collects'];
            $info['zans'] = $article_info['zans'];
            $info['zan_money'] = $article_info['zan_money'];
            $info['click'] = $article_info['click'];
            $info['praise'] = $article_info['praise'];
            //获取发布长文章时生成的圈子的信息
            $info['circle_info'] = $this->mcircle->get_one(array('t_id'=>$article_id,'c_type'=>2,'rid'=>0));

            //浏览量+1
            $this->mlarticle->set($article_id,array('click','click + 1'));
            //热门指数
            $views = $article_info['click'] + 1;
            $share = $article_info['shares'];
            $hots = $views + $share*10;
            $this->mlarticle->set($article_id,array('hots',$hots));

            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $info;
        }
        $this->_send_json($this->vdata);
    }
    /**
     * 长文章详情（发布人）
     */
    public function longarticle_detailh5(){
        $article_id = $this->input->post('id');
        $article_info = $this->mlarticle->get_one(array('id'=>$article_id,'is_deleted'=>0));
        if(empty($article_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '文章不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            if($article_info['uid']==$this->userinfo['id']){
                $info['is_can']=1;
            }else{
                $info['is_can']=0;
            }
            //是否收藏
            $my_collect = $this->mcollects->get_one(array('uid'=>$this->userinfo['id'],'tid'=>$article_id,'is_deleted'=>1));
            if(!empty($my_collect)){
                $info['is_collect']=0;
            }else{
                $info['is_collect']=1;
            }
            //是否点赞
            $my_concerns = $this->mpraise->get_one(array('uid'=>$this->userinfo['id'],'tid'=>$article_id,'is_deleted'=>1));
            if(!empty($my_concerns)){
                $info['is_praise']=0;
            }else{
                $info['is_praise']=1;
            }
            $text_users = array();
            $text_zans = $this->db->query("select * from boc_text_zan where tid=$article_id order by created desc limit 5")->result_array();
            if(!empty($text_zans)){
                foreach($text_zans as $val){
                    $zan_user_info = $this->maccount->get_one(array('id'=>$val['uid']));
                    $u_photo = $this->mupload->get_one(array('id'=>$zan_user_info['photo']));
                    if(!empty($u_photo)){
                        $text_users[] = UPLOAD_URL.$u_photo['url'];
                    }else{
                        $text_users[] = '';
                    }
                }
            }
            $info['zan_user'] = $text_users;
            $info['content'] = $article_info['content'];
            $info['title'] = $article_info['title'];
            $user_info = $this->maccount->get_one(array('id'=>$article_info['uid']));
            $info['uname'] = $user_info['uname'];
            $photo = $this->mupload->get_one(array('id'=>$user_info['photo']));
            if(!empty($photo)){
                $info['headimg'] = UPLOAD_URL.$photo['url'];
            }else{
                $info['headimg'] = '';
            }
            $info['id'] = $article_id;
            $info['cre_time'] = date('m-d H:i',$article_info['timeline']);
            $info['repeats'] = $article_info['repeats'];
            $info['uid'] = $article_info['uid'];
            $info['comments'] = $article_info['comments'];
            $info['collects'] = $article_info['collects'];
            $info['zans'] = $article_info['zans'];
            $info['zan_money'] = $article_info['zan_money'];
            $info['click'] = $article_info['click'];
            $info['praise'] = $article_info['praise'];
            //获取发布长文章时生成的圈子的信息
            $info['circle_info'] = $this->mcircle->get_one(array('t_id'=>$article_id,'c_type'=>2,'rid'=>0));

            //浏览量+1
            $this->mlarticle->set($article_id,array('click','click + 1'));
            //热门指数
            $views = $article_info['click'] + 1;
            $share = $article_info['shares'];
            $hots = $views + $share*10;
            $this->mlarticle->set($article_id,array('hots',$hots));

            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $info;
        }
        $this->_send_json($this->vdata);
    }
    /**
     * 评论详情列表展示(下拉加载)
     */
    public function circle_comments(){
        $id = $this->input->post('id');
        $type = $this->input->post('type');
        $start = $this->input->post('page');
        $limit = 5;
        $start = $limit*($start-1);
        $comments = $this->mcomments->get_list($limit,$start,array('created'=>'desc'),array('rid'=>$id,'type'=>$type,'pid'=>0,'audit'=>1),"*");
        //获取当前用户信息
        $user_info = $this->userinfo;
        $uid = $user_info['id'];
        if(empty($comments)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            foreach($comments as $kk=>$vv){
                if($vv['uid']==$uid){
                    $comments[$kk]['is_can']=0;
                }else{
                    $comments[$kk]['is_can']=1;
                }
                $user_info = $this->maccount->get_one(array('id'=>$vv['uid']));
                $comments[$kk]['uname']=$user_info['uname'];
                $comments[$kk]['content'] = userTextDecode($vv['content']);
                $photo = $this->mupload->get_one(array('id'=>$user_info['photo']));
                if(!empty($photo)){
                    $comments[$kk]['headimg'] = UPLOAD_URL.$photo['url'];
                }else{
                    $comments[$kk]['headimg'] = '';
                }
                $comments[$kk]['cre_time'] = date('m-d H:i',$vv['created']);
                $child = $this->mcomments->get_all(array('rid'=>$id,'type'=>$type,'pid'=>$vv['id'],'audit'=>1),'*',array('created'=>'asc'));
                foreach($child as $key=>$val){
                    $user_info = $this->maccount->get_one(array('id'=>$val['uid']));
                    $user_info2 = $this->maccount->get_one(array('id'=>$val['chat_id']));
                    $child[$key]['uname']=$user_info['uname'];
                    $child[$key]['content'] = userTextDecode($val['content']);
                    if(!empty($user_info2)){
                        $child[$key]['cname']=$user_info2['uname'];
                    }else{
                        $child[$key]['cname']='';
                    }
                    if($val['uid']==$uid){
                        $child[$key]['is_can']=0;
                    }else{
                        $child[$key]['is_can']=1;
                    }
                }
                $comments[$kk]['child']=$child;
                $comments[$kk]['childnums']=count($child);
            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $comments;
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 转发长文章
     */
    public function repeat_larticle(){
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        //获取当前用户信息
        $user_info = $this->userinfo;
        if(!isset($user_info) && empty($user_info)){
            $this->vdata['returnCode']   = '401';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            $uid = $user_info['id'];
            $data = $this->input->post();
            if(!isset($data) && empty($data)){
                $this->vdata['returnCode']   = '250';
                $this->vdata['returnInfo'] = '缺少参数';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                $r_content = $this->input->post('r_content');
                $r_content1 = $this->input->post('r_content1');
                if(!isset($content) || trim($content)==''){
                    $this->vdata['returnCode']   = '250';
                    $this->vdata['returnInfo'] = '请填写分享心得';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array();
                }
                $tid = $data['tid'];  //长文章ID
                $rid = $data['rid'];
                $create_data = array(
                    'uid'=>$uid,
                    't_id'=>$tid,    //长文章ID
                    'rid'=>$rid,    //转发圈子的ID
                    'c_type'=>2,
                    'r_content1'=>userTextEncode(strip_tags($r_content1)),
                    'r_content'=>userTextEncode(strip_tags(trim($r_content))),
                    'timeline'=>time()
                );
                $circle_id = $this->mcircle->create($create_data);
                if($circle_id){
                    //转发成功之后将上一条的转发圈子的转发数+1
                    if($rid!=0){
                        $this->mcircle->set($rid,array('repeats','repeats + 1'));
                    }
                    $this->mlarticle->set($tid,array('repeats','repeats + 1'));
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '转发成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $circle_id;
                }else{
                    $this->vdata['returnCode']   = '250';
                    $this->vdata['returnInfo'] = '转发失败';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array();
                }
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 赞赏限额信息
     */
    public function maxmoney(){
        $maxmoney = $this->mmaxmoney->get_one(array('id'=>2));
        $this->vdata['returnCode']   = '200';
        $this->vdata['returnInfo'] = '请求成功';
        $this->vdata['secure']     = JSON_SECURE;
        $this->vdata['content']= $maxmoney;
        $this->_send_json($this->vdata);
    }

    /**
     * 长文章赞赏kind为2 打赏
     */
    public function recharge_zan(){
        $maxmoney = $this->mmaxmoney->get_one(array('id'=>2));
        if(intval($this->data['money']) < $maxmoney['min_money']){
            $this->vdata['returnCode'] = '250';
            $this->vdata['returnInfo'] = '赞赏金额至少为一元';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array();
        }elseif(intval($this->data['money']) > $maxmoney['max_money']){
            $this->vdata['returnCode'] = '250';
            $this->vdata['returnInfo'] = '赞赏金额不能大于'.$maxmoney['max_money'];
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array();
        }else{
            $pay_code = get_order_sn('boc_pay_logs','L');
            $data = array(
                'uid' => $this->userinfo['id'],
                'money' => $this->data['money'],
                'type' => $this->data['type'],
                'tid' => $this->data['tid'],
                'kind' => 2,
                'pay_code'=>$pay_code,
                'created' => time()
            );
            $larticle_id = $this->data['tid'];
            $detail = $this->mpay_log->create($data);
            if($this->data['type']==3){
                if($detail){
                    //使用余额支付 查询余额是否充足
                    $balance = $this->userinfo['balance'];
                    $money = $this->data['money'];
                    $money = (int)$money;
                    if($balance >=$money){
                        $s_balance = $balance-$this->data['money'];   //扣除余额（赞赏支出）
                        $this->maccount->update(array('balance'=>$s_balance),array('id'=>$this->userinfo['id']));
                        //生成交易单号
                        $payment_sn = ordernum_time('',true);
                        //获取长文章作者的信息
                        $larticle_info = $this->mlarticle->get_one(array('id'=>$this->data['tid'],'is_deleted'=>0));
                        $to_uid = $larticle_info['uid'];
                        $this->db->query("update boc_account set balance=balance+$money,zan_money=zan_money+$money where id=$to_uid");
                        $balance_data_out = array(
                            'uid'=>$this->userinfo['id'],
                            'type'=>1,
                            'title'=>$larticle_info['title'],
                            'money'=>$money,
                            'status'=>1,
                            'pay_ways'=>3,
                            'pay_time'=>time(),
                            'created_at'=>time(),
                            'rid'=>$to_uid,
                            'payment_sn'=>$payment_sn,
                            'pay_code'=>$pay_code,
                        );
                        $this->mbalance->create($balance_data_out);
                        //赞赏收入
                        $balance_data_in = array(
                            'uid'=>$to_uid,
                            'type'=>2,
                            'title'=>$larticle_info['title'],
                            'money'=>$money,
                            'status'=>1,
                            'pay_ways'=>3,
                            'pay_time'=>time(),
                            'created_at'=>time(),
                            'rid'=>$this->userinfo['id'],
                            'payment_sn'=>$payment_sn,
                            'pay_code'=>$pay_code,
                        );
                        $this->mbalance->create($balance_data_in);
                        $zan_data = array(
                            'uid'=>$this->userinfo['id'],
                            'tid'=>$this->data['tid'],
                            'money'=>$this->data['money'],
                            'created'=>time()
                        );
                        $this->mtzan->create($zan_data);
                        $this->db->query("update boc_long_article set zans=zans+1,zan_money=zan_money+$money where id=$larticle_id");
                        /*******添加推送板块**********/
                        $uname = $this->userinfo['uname'];
                        $t_type = 3;
                        $content = $uname.'赞赏了您';
                        getui_push($to_uid,$t_type,$larticle_id,$content);


                        $this->vdata['returnCode'] = '200';
                        $this->vdata['returnInfo'] = '成功打赏';
                        $this->vdata['secure'] = JSON_SECURE;
                        $this->vdata['content'] = array('order_id'=>$detail,'order_no'=>$data['pay_code'],'type'=>$data['type']);
                    }else{
                        $this->vdata['returnCode'] = '250';
                        $this->vdata['returnInfo'] = '余额不足';
                        $this->vdata['content'] = '';
                    }
                }else{
                    $this->vdata['returnCode'] = '250';
                    $this->vdata['returnInfo'] = '操作失败';
                    $this->vdata['content'] = '';
                }
            }else{
                if($detail){
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作成功';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content'] = array('order_id'=>$detail,'order_no'=>$data['pay_code'],'money'=>$data['money'],'type'=>$data['type']);
                }else{
                    $this->vdata['returnCode'] = '250';
                    $this->vdata['returnInfo'] = '操作失败';
                    $this->vdata['content'] = '';
                }
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 删除长文章
     */
    public function delete_article(){
        //获取当前用户信息
        $user_info = $this->userinfo;
        $uid = $user_info['id'];
        $rid = $this->input->post('rid');
        $larticle_info = $this->mlarticle->get_one(array('id'=>$rid,'is_deleted'=>0));
        if(empty($larticle_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '记录不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else {
            if($larticle_info['uid']==$uid){
                $this->db->query("update boc_long_article set is_deleted=1 where id=$rid and uid=$uid");
                $this->db->query("update boc_circle set is_deleted=1 where c_type=2 and t_id=$rid and uid=$uid and rid=0");
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '删除成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                $this->vdata['returnCode']   = '250';
                $this->vdata['returnInfo'] = '没有权限';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 判断余额是否充足
     */
    public function balance_empty(){
        $money = $this->input->post('money');
        $balance = $this->userinfo['balance'];
        if(!isset($money)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '缺少参数';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            if($money <= $balance){
                $info['is_empty'] = 1;
                $info['balance'] = $balance;
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '操作成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= $info;
            }else{
                $info['is_empty'] = 0;
                $info['balance'] = $balance;
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '操作成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= $info;
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 长文章分享数更新分享数和热门指数（浏览量+分享数*10）
     */
    public function shares(){
        $tid = $this->input->post('tid');
        $larticle_info = $this->mlarticle->get_one(array('id'=>$tid,'is_deleted'=>0));
        if(empty($larticle_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '记录不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            $this->mlarticle->set($tid,array('shares','shares + 1'));
            $views = $larticle_info['click'];
            $share = $larticle_info['shares'] + 1;
            $hots = $views + $share*10;
            $this->mlarticle->set($tid,array('hots',$hots));
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '请求成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }
        $this->_send_json($this->vdata);
    }

}